﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using PPI.Data.Inspection;
using PPI.Entities.Interface;
using PPI.Mobile;
using VMind.Global;

namespace PPI.ServiceLogic.Distribution
{
    public class LMap : IMobileTable
    {
        public static DataSet SelectAll(string connectionKey)
        {
            return DInspectionMap.SelectAll(connectionKey);
        }

        public List<EMobileQuery> CreateInsertMobileQuery(string connectionKey)
        {
            List<EMobileQuery> listQueries = new List<EMobileQuery>();
            StringBuilder _Sql = new StringBuilder();
            DataTable _Dt = SelectAll(connectionKey).Tables[0];
            int lastId = 0;

            listQueries.Add(new EMobileQuery("DELETE tb_Utility_Polygons"));

            foreach (DataRow dr in _Dt.Rows)
            {
                int polygonType;
                string parentId;

                if (ConvertHelper.ToInt32(dr["PolygonParentId"]) == Int32.MinValue)
                {
                    polygonType = 1; //MAP
                    parentId = "NULL";
                }
                else
                {
                    polygonType = 2; //SECTION
                    parentId = dr["PolygonParentId"].ToString();
                }

                lastId = ConvertHelper.ToInt32(dr["PolygonChildId"]);
                listQueries.Add(new EMobileQuery("INSERT INTO tb_Utility_Polygons (PolygonId, ParentPolygonFk, PolygonTypeFk, PolygonName) VALUES (" + lastId + "," + parentId + "," + polygonType + ",'" + dr["PolygonChildName"] + "')"));
            }

            for (int i = 1; i < 37; i++)
                listQueries.Add(new EMobileQuery("INSERT INTO tb_Utility_Polygons (PolygonId, ParentPolygonFk, PolygonTypeFk, PolygonName) VALUES (" + (lastId + i) + ", NULL, 2,'" + i + "')"));
 
            return listQueries;
        }
    }
}
